package com.itheima.reggie.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.ISetmealService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

//套餐
@Service
@Slf4j
@SuppressWarnings("unchecked")
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        // 1.先查套餐基本信息
        // 1-1 构建条件对象
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.like(StrUtil.isNotEmpty(name), Setmeal::getName, name);
        // 1-2 构建分页对象
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        // 1-3 查询
        page = setmealMapper.selectPage(page, setmealWrapper);

        // 2.获取套餐list集合并遍历
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)) {
            for (Setmeal setmeal : setmealList) {
                // 3.根据category_id查询分类对象
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                // 将分类名称关联到套餐中
                setmeal.setCategoryName(category.getName());
                // 4.根据setmeal的id查询菜品（中间表）列表
                // 4-1 构建中间表条件对象
                LambdaQueryWrapper<SetmealDish> sdWrapper = new LambdaQueryWrapper<>();
                sdWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
                // 4-2  查询套菜菜品集合
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(sdWrapper);
                // 4-3 关联到套餐中
                setmeal.setSetmealDishes(setmealDishList);
            }
        }
        // 5.返回结果
        return page;
    }

    // 套餐新增
    @Override
    @CacheEvict(value = "setmeal",key = "#setmeal.categoryId")
    public void saveSetmeal(Setmeal setmeal) {
        // 1.先保存套餐基本信息
        setmealMapper.insert(setmeal);
        log.info("保存套餐基本信息，id：{},名称：{},价格：{}", setmeal.getId(), setmeal.getName(), setmeal.getPrice());

        // 2.取出套餐菜品列表
        List<SetmealDish> dishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (SetmealDish setmealDish : dishList) {
                // 关联套餐id
                setmealDish.setSetmealId(setmeal.getId());
                // 保存套餐菜品
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
//    @Override
//    public void saveSetmale(Setmeal setmeal) {
//        // 1.先保存套餐基本信息
//        setmealMapper.insert(setmeal);
//        log.info("保存套餐基本信息，id：{},名称：{},价格：{}", setmeal.getId(),setmeal.getName(),setmeal.getPrice());
//        // 2.取出套餐菜品列表
//        List<SetmealDish> dishList = setmeal.getSetmealDishes();
//        if (CollectionUtil.isNotEmpty(dishList)) {
//            for (SetmealDish setmealDish : dishList) {
//                // 关联套餐id
//                setmealDish.setSetmealId(setmeal.getId());
//                // 保存套餐菜品
//                setmealDishMapper.insert(setmealDish);
//            }
//        }
//    }

    // h5页面套餐展示
    @Override
    @Cacheable(value = "setmeal",key = "#categoryId")
    public List<Setmeal> setmealList(Long categoryId, Integer status) {
        System.out.println("查询mysql数据库..");
        // 1.构建条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId);
        wrapper.eq(Setmeal::getStatus, status);
        // 2.执行查询
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        // 3.返回结果
        return setmealList;
    }

    @Override
    public  List<SetmealDish> setMealDishDetails(Long id) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);



        for (SetmealDish setmealDish : setmealDishes) {

            LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
            dishWrapper.eq(Dish::getId,setmealDish.getDishId());
            setmealDish.setImage(dishMapper.selectOne(dishWrapper).getImage());

        }

        return setmealDishes;
    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        // 条件对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        // 修改实体
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        // 调用mapper
        setmealMapper.update(setmeal, wrapper);
    }

    @Override
    public Setmeal findById(Long id) {
        // 1.先查菜品基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
        // 2.在查询口味列表
        // 2-1 构建口味的查询条件对象
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        // 2-2查询列表
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(wrapper);
        // 3.将口味列表设置到菜品对象中
        setmeal.setSetmealDishes(setmealDishes);
        // 4.返回菜品对象
        return setmeal;
    }

//    @Override
//    public void updateSetmeal(Setmeal setmeal) {
//        // 1.先更新菜品基本信息
//        setmealMapper.updateById(setmeal);
//
//        // 2.删除菜品原有的口味
//        // 2-1 构建口味条件对象
//        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(SetmealDish::getSetmealId ,setmeal.getId());
//        // 2-2 执行mapper删除
//        setmealDishMapper.delete(wrapper);
//
//        // 3.遍历前端提交的口味
//        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
//        if (CollectionUtil.isNotEmpty(setmealDishes)) {
//            for (SetmealDish setmealdish : setmealDishes) {
//                // 设置菜品id
//                setmealdish.setSetmealId(setmeal.getId());
//                // 调用mapper保存口味
//                setmealDishMapper.insert(setmealdish);
//            }
//        }
//    }
// 套餐更新
    @Override
    @CacheEvict(value = "setmeal",allEntries = true)
    public void updateSetmeal(Setmeal setmeal) {
        //1. 根据主键更新菜品表信息
        setmealMapper.updateById(setmeal);

        //2. 根据套餐id删除中间表中相关菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //3. 向中间表保存新的菜品列表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
}


}
